<?php
/**
 * Portal class
 * Represents whole service, makes controllers and dispatches front controller
 * 
 * @author paq
 *
 */
class Portal
{
	/**
	 * 
	 * @var Controller_Front
	 */
	private $_frontController;
	
	public function __construct()
	{
		$this->setUpControllers();
	}
	
	/**
	 * Sets allowed controllers
	 * 
	 * @return void
	 */
	protected function setUpControllers()
	{
		Log::info('Setting up front controller and rest controllers');
		
		$this->_frontController = new Controller_Front();
		
		// SIMPLE CONTROLLERS
		$this->_frontController->register(new Controller_Index($this->_frontController));
		$this->_frontController->register(new Controller_News($this->_frontController));
		$this->_frontController->register(new Controller_Login($this->_frontController));
		$this->_frontController->register(new Controller_Register($this->_frontController));
	    $this->_frontController->register(new Controller_Vortal($this->_frontController));
	    $this->_frontController->register(new Controller_Search($this->_frontController));
	    $this->_frontController->register(new Controller_Statistic($this->_frontController));
	    $this->_frontController->register(new Controller_Category($this->_frontController));
	    
	    // ADMIN PANEL CONTROLLERS
		$this->_frontController->register(new Controller_Admin($this->_frontController));
		$this->_frontController->register(new Controller_Admin_Category($this->_frontController));
		$this->_frontController->register(new Controller_Admin_ContentBlock($this->_frontController));
		$this->_frontController->register(new Controller_Admin_News($this->_frontController));
		$this->_frontController->register(new Controller_Admin_Producer($this->_frontController));
		$this->_frontController->register(new Controller_Admin_Product($this->_frontController));
		$this->_frontController->register(new Controller_Admin_User($this->_frontController));
		$this->_frontController->register(new Controller_Admin_Vortal($this->_frontController));
		$this->_frontController->register(new Controller_Admin_RequestStatus($this->_frontController));
		$this->_frontController->register(new Controller_Admin_ReviewRequest($this->_frontController));
			
		// USER PANLE CONTROLLERS
		$this->_frontController->register(new Controller_User_News($this->_frontController));
		$this->_frontController->register(new Controller_User($this->_frontController));	
		$this->_frontController->register(new Controller_User_Reviewrequest($this->_frontController));
	
	}
	
	/**
	 * Runs portal
	 * 
	 * @return void
	 */
	public function run()
	{
		Log::info('Running portal');
		
		if (isset($_GET['portal']) && $_GET['portal'] == 'initialize') {
			$this->initialize();
		}
		
		$this->_frontController->dispatch();
	}
	
	protected function initialize()
	{
		Manager_RequestStatus::initialize();
		Manager_Category::initialize();
		Manager_PreferenceGroup::initialize();
		Manager_SiteConfig::initialize();
		Manager_User::initialize();
		Manager_Vortal::initialize();
	}
	
	public function testPropel()
	{
		$prod = new Producers();
		$prod->setName('poooo');
		$prod->setUrl('http://csdcsd');
		try {
			$prod->save();
			Log::info('Propel test ok');
		} catch (Exception $e) {
			Log::info('propel test FAILED! ' . $e->getMessage());
		}
	}
}